<template>
  <div>
    <el-dialog :title="dialogTitle" :visible="showDialog" @close="closeFn">
      <el-form ref="form" label-width="120px" :rules="rules" :model="form">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="form.name" placeholder="2-10个字符" style="width: 80%" />
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input v-model="form.code" placeholder="2-10个字符" style="width: 80%" />
        </el-form-item>
        <el-form-item label="部门负责人" prop="managerId">
          <el-select v-model="form.managerId" placeholder="请选择活动区域" style="width: 80%">
            <el-option
              v-for="item in managerList"
              :key="item.id"
              :label="item.username"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input
            v-model="form.introduce"
            type="textarea"
            style="width: 80%"
            :rows="4"
            placeholder="1-100个字符"
          />
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="12">
              <el-button size="mini" type="primary" @click="addcloseFn">确 定</el-button>
              <el-button size="mini" @click="closeFn">取 消</el-button>
            </el-col>
          </el-row>

        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getManagerList, addDept, getDepDetail, editDept } from '@/api/department'
export default {
  props: {
    showDialog: Boolean,
    originDepe: {
      type: Array,
      required: true
    },
    currentNodeId: {
      type: [Number, String],
      required: true
    }
  },
  data() {
    return {
      managerList: [],
      form: {
        name: '',
        code: '',
        managerId: '',
        introduce: ''

      },
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 2, max: 10, message: '部门名称2-10位', trigger: 'blur' },
          { validator: (rule, value, callback) => {
            // 直接调用 callback 表示效验通过
            // 如果表示效验失败，callback(new Error('失败信息'))
            let deps = this.originDepe
            if (this.form.id) {
              deps = deps.filter(item => item.id !== this.form.id)
            }
            const res = deps.some(item => item.name === value)
            res ? callback(new Error('部门信息重复')) : callback()
          },
          trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { min: 2, max: 10, message: '部门编码2-10位', trigger: 'blur' },
          { validator: (rule, value, callback) => {
            // 直接调用 callback 表示效验通过
            // 如果表示效验失败，callback(new Error('失败信息'))
            let deps = this.originDepe
            if (this.form.id) {
              deps = deps.filter(item => item.id !== this.form.id)
            }
            const res = deps.some(item => item.code === value)
            res ? callback(new Error('部门编码重复')) : callback()
          },
          trigger: 'blur' }

        ],
        managerId: [
          { required: true, message: '请输入名称', trigger: 'change' }
        ],
        introduce: [
          { required: true, message: '请输入简介', trigger: 'blur' },
          { min: 1, max: 100, message: '部门名称2-10位', trigger: 'blur' }
        ]

      }
    }
  },
  computed: {
    dialogTitle() {
      return `${this.form.id ? '编辑部门' : '新增部门'}`
    }
  },
  created() {
    this.fn()
  },
  methods: {
    fn() {
      getManagerList().then(res => {
        this.managerList = res
        // console.log(res)
      })
    },
    closeFn() {
      this.form = {
        name: '',
        code: '',
        managerId: '',
        introduce: ''

      }
      this.$refs.form.resetFields()
      this.$emit('update:showDialog', false)
    },
    addcloseFn() {
      this.$refs.form.validate(async pass => {
        if (pass) {
          this.form.id
            ? await editDept({ ...this.form })
            : await addDept({
              ...this.form, pid: this.currentNodeId
            })
          this.$emit('updataDeps')
          this.$message.success(`${this.form.id ? '编辑' : '新增'}成功`)
          this.closeFn()
        }
      })
    },
    async editlist() {
      this.form = await getDepDetail(this.currentNodeId)
      // console.log(res)
    }

  }
}

</script>

<style>

</style>
